/* <tools/nycr/symbol/output_file.cc>

   Implements <tools/nycr/symbol/output_file.h>.

   Copyright 2010-2014 OrlyAtomics, Inc.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License. */

#include <tools/nycr/symbol/output_file.h>

#include <base/no_default_case.h>
#include <base/thrower.h>

using namespace std;
using namespace Tools::Nycr;
using namespace Tools::Nycr::Symbol;

void TPath::Write(ostream &strm) const {
  assert(this);

  strm<< Branch << '/' << Atom << '.' << TLower(Language->GetName());
}

void TScope::Write(ostream &strm) const {
  assert(this);
  const TLanguage::TNamespaces &namespaces = Language->GetNamespaces();
  for (auto iter = namespaces.begin(); iter != namespaces.end(); ++iter) {
    strm << TUpper(*iter) << "::";
  }
}

void TUsingNamespace::Write(ostream &strm) const {
  assert(this);
  const TLanguage::TNamespaces &namespaces = Language->GetNamespaces();
  if (!namespaces.empty()) {
    strm << "using namespace ";
    for (auto iter = namespaces.begin(); iter != namespaces.end(); ++iter) {
      if (iter != namespaces.begin()) {
        strm << "::";
      }
      strm << TUpper(*iter);
    }
    strm << ';' << endl;
  }
}

void TUnderscore::Write(ostream &strm) const {
  assert(this);
  const TLanguage::TNamespaces &namespaces = Language->GetNamespaces();
  if (!namespaces.empty()) {
    for (auto iter = namespaces.begin(); iter != namespaces.end(); ++iter) {
      if (iter != namespaces.begin()) {
        strm << "_";
      }
      strm << TLower(*iter);
    }
    strm << "__";
  }
}

void Tools::Nycr::Symbol::CreateOutputFile(const char *root, const char *branch, const char *atom, const TLanguage *language, const char *ext, ofstream &strm, TCommentStyle comment_style) {
  assert(root);
  assert(language);
  assert(ext);
  assert(&strm);
  ostringstream temp;

  temp << root << '/' << atom << '.' << TLower(language->GetName()) << ext;

  string name = temp.str();
  strm.open(name);
  if (!strm.is_open()) {
    THROW << "could not open \"" << name << '"';
  }

  const char *comment_open;
  const char *comment_close;
  switch (comment_style) {
  case TCommentStyle::CStyle: {
    comment_open = "/*";
    comment_close = "*/";
    break;
  }
  case TCommentStyle::XmlStyle: {
    comment_open = "<!--";
    comment_close = "-->";
    break;
  }
    NO_DEFAULT_CASE;
  }

  strm
    << comment_open << " <" << TPath(branch, atom, language) << ext << ">" << endl << endl
    << "   This file was generated by nycr. " << comment_close << endl << endl;
}